package fr.ants.controller.command;

import fr.ants.controller.clock.Clock;
import fr.ants.model.Node;
import fr.ants.view.panel.Canvas;

/**
 * To delete a graphic component in the map.
 * 
 * @author Guillaume Ruscassie
 * 
 */
public class DeleteCommand implements Command {

	/**
	 * Canvas.
	 */
	private Canvas canvas;

	/**
	 * Clock.
	 */
	private final Clock clock;

	/**
	 * Build.
	 * 
	 * @param clock
	 *            clock.
	 */
	public DeleteCommand(final Clock clock) {
		this.clock = clock;
	}

	/**
	 * To delete a Resource or a Nest in the map.
	 * 
	 * @author Guillaume Ruscassie, Jordan ENSFELDER
	 * 
	 */
	@Override
	public void execute() {
		if (canvas.getSelectedGridX() != null
				&& canvas.getSelectedGridY() != null) {

			Node node = clock
					.getSimulator()
					.getGrid()
					.getNode(canvas.getSelectedGridX(),
							canvas.getSelectedGridY());

			// si on a une fourmillière
			if (node.getNest() != null) {
				node.getNest().getAnts().clear();
				node.setNest(null);
			}

			// si on a des fourmis
			if (node.getAnts() != null) {
				node.getAnts().clear();
				node.setNest(null);
			}

			// si il s'agit d'une Resource
			if (node.getResource() != null) {
				node.getResource().setCapacity(0);
				node.setResource(null);
			}

			// si on a un obstacle infranchissable
			if (!node.isWalkable()) {
				node.setDifficulty(0);
			}

			if (!clock.isRunning()) {
				canvas.repaint();
			}

		}
	}

	/**
	 * We need the canvas to execute this command.
	 * 
	 * @param canvas
	 *            canvas
	 */
	public void setCanvas(final Canvas canvas) {
		this.canvas = canvas;
	}

}
